package helpers;

import java.util.ArrayList;
import java.util.Arrays;

import models.FeedType;
import models.Genre;

public class ItuneXMLUrlHelper {
	static ItuneXMLUrlHelper instance = null;
	private String limit = "25";
	private String country = "us";
	private String type = "topfreeapplications";
	private String genreId = null;
	private ArrayList<Genre> genres = new ArrayList<Genre>();
	
	private ArrayList<FeedType> types = new ArrayList<FeedType>();
	
	private ArrayList<String> limits  = new ArrayList<String>();
	
	static ItuneXMLUrlHelper getInstance() {
		if(instance == null)
			instance = new ItuneXMLUrlHelper();
		return instance;
	}
	
	public ItuneXMLUrlHelper() {
		//Set type
		this.setTypes();
		//Set Limit
		this.setLimits();
		//Set genres
		this.setGenres();
	}
	
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public String getGenreId() {
		return genreId;
	}
	public void setGenreId(String genreId) {
		this.genreId = genreId;
	}
	public ArrayList<FeedType> getTypes() {
		return types;
	}
	
	public static FeedType getTypeByAlias(String alias) {
		ItuneXMLUrlHelper ituneXML = new ItuneXMLUrlHelper();
		ArrayList<FeedType> feedTypes = ituneXML.getTypes();
		FeedType feedType = new FeedType();
		
		for (int i = 0; i < feedTypes.size(); i++) {
			feedType = feedTypes.get(i);
			if(alias.equalsIgnoreCase(feedType.getAlias())) {
				return feedType;
			}
		} 
		
		return feedType;
	}
	
	public void setTypes() {
		FeedType feedType = new FeedType();
		//Top Free Apps
		feedType.setName("Top Free Apps");
		feedType.setAlias("topfreeapplications");
		types.add(feedType);
		
		//Top Paid Apps
		feedType = new FeedType();
		feedType.setName("Top Paid Apps");
		feedType.setAlias("toppaidapplications");
		types.add(feedType);
		
		//Top Grossing Apps
		feedType = new FeedType();
		feedType.setName("Top Grossing Apps");
		feedType.setAlias("topgrossingapplications");
		types.add(feedType);
		
		//Top Free iPad Apps
		feedType = new FeedType();
		feedType.setName("Top Free iPad Apps");
		feedType.setAlias("topfreeipadapplications");
		types.add(feedType);
		
		//Top Paid iPad Apps
		feedType = new FeedType();
		feedType.setName("Top Paid iPad Apps");
		feedType.setAlias("toppaidipadapplications");
		types.add(feedType);
		
		//Top Grossing iPad Apps
		feedType = new FeedType();
		feedType.setName("Top Grossing iPad Apps");
		feedType.setAlias("topgrossingipadapplications");
		types.add(feedType);
		
		//New Apps
		feedType = new FeedType();
		feedType.setName("New Apps");
		feedType.setAlias("newapplications");
		types.add(feedType);
		
		//New Free Applications
		feedType = new FeedType();
		feedType.setName("New Free Applications");
		feedType.setAlias("newfreeapplications");
		types.add(feedType);
		
		//New Paid Applications
		feedType = new FeedType();
		feedType.setName("New Paid Applications");
		feedType.setAlias("newpaidapplications");
		types.add(feedType);
		
	}
	public ArrayList<String> getLimits() {
		return limits;
	}
	public void setLimits() {
		this.limits  = new ArrayList<String>(Arrays.asList(
				"10", "25", "50", "100", "200", "300", "400"));
	}
	public String getUrl() {
		String url = "http://itunes.apple.com/" + country  + "/rss/" + type + "/limit=" + limit;
		if (genreId != null && !genreId.equalsIgnoreCase("0")) {
			url += "/genre=" + genreId;
		}
		url += "/xml";
		System.out.println("url: " + url);
		return url;
	}

	public void setGenres() {
		Genre genre = new Genre();
		
		genre = new Genre();
		genre.setId("0");
		genre.setName("All Genres");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6018");
		genre.setName("Books");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6000");
		genre.setName("Business");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6022");
		genre.setName("Catalogues");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6017");
		genre.setName("Education");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6016");
		genre.setName("Entertainment");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6015");
		genre.setName("Finance");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6014");
		genre.setName("Games");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6013");
		genre.setName("Health & Fitness");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6012");
		genre.setName("Lifestyle");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6020");
		genre.setName("Medical");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6011");
		genre.setName("Music");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6010");
		genre.setName("Navigation");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6009");
		genre.setName("News");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6021");
		genre.setName("Newsstand");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6008");
		genre.setName("Photo & Video");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6007");
		genre.setName("Productivity");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6006");
		genre.setName("Reference");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6005");
		genre.setName("Social Networking");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6004");
		genre.setName("Sports");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6003");
		genre.setName("Travel");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6002");
		genre.setName("Utilities");
		this.genres.add(genre);
		
		genre = new Genre();
		genre.setId("6001");
		genre.setName("Weather");
		this.genres.add(genre);
	}

	public ArrayList<Genre> getGenres() {
		return genres;
	}
	
	public static Genre getGenreById(String id) {
		ItuneXMLUrlHelper ituneXML = new ItuneXMLUrlHelper();
		ArrayList<Genre> genres = ituneXML.getGenres();
		Genre genre = new Genre();
		for (int i = 0; i < genres.size(); i++) {
			genre = genres.get(i);
			if (id.equalsIgnoreCase(genre.getId())) {
				return genre;
			}
		}
		return genre;
	}	
	
	public static String createAppURL(String name, String id) {
		String url = StringHelper.getAlias(name) + "_" + id + ".html";
		return url;
	}
}